<?php
// No direct access
defined('_JEXEC') or die;

class JE_ContentModelContent extends JModel
{
	function getItem()
	{
		$db = JFactory::getDbo();
		$id = JRequest::getVar('id');
		
		// Filter by start and end dates.
		$nullDate = $db->Quote($db->getNullDate());
		$nowDate = $db->Quote(JFactory::getDate()->toMySQL());
		
		$query  = ' SELECT a.* ';
		$query .= ' FROM #__je_content a ';
		$query .= " WHERE a.state = 1 ";
		$query .= ' AND (a.publish_up = '.$nullDate.' OR a.publish_up <= '.$nowDate.')';
		$query .= ' AND (a.publish_down = '.$nullDate.' OR a.publish_down >= '.$nowDate.')';
		$query .= ' AND a.id = '. (int) $id;
		
		$db->setQuery($query);
		$result = $db->loadObject();
		
		return $result;
	}
	
	/**
	 * Function to get random other contents 
	 */
	public function getOtherContents($id, $limit = 10)
	{
		$db = JFactory::getDbo();
		
		// Filter by start and end dates.
		$nullDate = $db->Quote($db->getNullDate());
		$nowDate = $db->Quote(JFactory::getDate()->toMySQL());
		
		$query = "	SELECT DISTINCT a.id, a.*
					FROM #__je_content a 
					JOIN #__je_content_categories c ON a.id = c.cat_id 
					WHERE a.catid = (SELECT catid FROM #__je_content WHERE id = '$id') 
							AND a.id != '$id' 
							AND a.state = 1
							AND (a.publish_up = $nullDate OR a.publish_up <= $nowDate)
							AND (a.publish_down = $nullDate OR a.publish_down >= $nowDate)
					ORDER BY RAND()";
		
		$db->setQuery($query);
		$result = $db->loadObjectList();
		
		return $result;
	} 
}